package com.imay.live.activity.discovery;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.imay.base.util.StringUtil;
import com.imay.live.activity.RoomLiveNormalActivity;
import com.imay.live.adapter.DiscoveryAdapter;
import com.imay.live.model.RoomInfoModel;
import com.imay.live.proto.Result;
import com.imay.live.proto.helper.ProtoCallBack;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.R;

import java.util.ArrayList;

/**
 * Created by liangzhenxiong on 2017/04/10 0010.
 * 发现-分类-标签详情列表
 */

public class HotLabelDetailActivity extends DicLoadActivity {
    final static String TITLE = "title";
    String label;


    public static void start(Context context, String title) {
        Intent starter = new Intent(context, HotLabelDetailActivity.class);
        starter.putExtra(TITLE, title);
        context.startActivity(starter);
    }

    @Override
    public void onRefreshLoad() {
        ProtoRequestHelper.task(ProtoHttpFactory.getLabelRooms(label, 0, mpapecount)).setTag(this.hashCode()).request(new ProtoCallBack() {
            @Override
            public void onRequestSuccess(String command, String data) {
                try {
                    Result<ArrayList<RoomInfoModel>> result = JSON.parseObject(data, new TypeReference<Result<ArrayList<RoomInfoModel>>>() {
                    }.getType());
                    loadViewControl.refreshLoadView(result.data, result.result);

                } catch (Exception e) {
                    loadViewControl.loadError(getResources().getString(R.string.str_loaderror));
                }


            }

            @Override
            public void onRequestError(String command, int code, String message) {
                loadViewControl.loadError(message);
            }
        });
    }

    @Override
    public void onLoadMore() {
        ProtoRequestHelper.task(ProtoHttpFactory.getLabelRooms(label, mpapecount, mpapecount)).setTag(this.hashCode()).request(new ProtoCallBack() {
            @Override
            public void onRequestSuccess(String command, String data) {
                try {
                    Result<ArrayList<RoomInfoModel>> result = JSON.parseObject(data, new TypeReference<Result<ArrayList<RoomInfoModel>>>() {
                    }.getType());
                    loadViewControl.moreLoadView(result.data, result.result);

                } catch (Exception e) {
                    loadViewControl.loadError(getResources().getString(R.string.str_loaderror));
                }


            }

            @Override
            public void onRequestError(String command, int code, String message) {
                loadViewControl.loadError(message);
            }
        });
    }

    @Override
    protected void initAdapte() {
        super.initAdapte();
        mpapecount = 30;
        mAdapter = new DiscoveryAdapter(mdatas);
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.discover_live_rl_square) {
                    RoomLiveNormalActivity.startActivity(HotLabelDetailActivity.this, (ArrayList<RoomInfoModel>) adapter.getData(), position);
                }
//                else if (view.getId() == R.id.discover_live_tv_nick) {
//                    RoomInfoModel roomInfoModel = (RoomInfoModel) adapter.getItem(position);
//                    if (roomInfoModel.getUser() != null) {
//                        ProfileActivity.start(HotLabelDetailActivity.this, roomInfoModel.getUser().getUid());
//                    }
//                }
            }
        });

    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        mrecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
    }

    @Override
    protected void initCreate(Bundle savedInstanceState) {
        super.initCreate(savedInstanceState);
        String title = getIntent().getStringExtra(TITLE);
        setTitle(title);
        if (!StringUtil.isEmpty(title)) {
            label = title.replaceAll("#", "");
        }
        onRefreshLoad();
    }

}
